package com.example.myapplication01.jetpack.room;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myapplication01.R;

import java.util.ArrayList;
import java.util.List;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {

    private List<User> list = new ArrayList<>();

    public void setList(List<User> list) {
        this.list = list;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_room_item, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        User user = list.get(position);
        holder.tvId.setText(String.valueOf(position + 1));
        holder.tvName.setText(user.getName());
        holder.tvAge.setText(String.valueOf(user.getAge()));
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    static class MyViewHolder extends RecyclerView.ViewHolder {
        TextView tvId, tvName, tvAge;

        public MyViewHolder(View itemView) {
            super(itemView);
            tvId = itemView.findViewById(R.id.textViewId);
            tvName = itemView.findViewById(R.id.textViewName);
            tvAge = itemView.findViewById(R.id.textViewAge);
        }
    }
}
